package com.hqyj.system.controller;

import com.hqyj.system.model.Role;
import com.hqyj.system.model.User;
import com.hqyj.system.model.UserRole;
import frame.controller.BaseController;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import java.util.List;

/**
 * @ClassName RoleController
 * @Description
 * @Author zlf
 * @Date 2021/9/14 10:14
 * @Version 1.0
 **/
@Controller
@RequestMapping("/roleController")
public class RoleController extends BaseController {
    @RequestMapping("/list.do")
    public String list(HttpServletRequest request, HttpServletResponse response, HttpSession session, Model model, Role role) {
        //1.接收数据;
        //3.调用业务;
        List<Role> role_dbList = roleService.queryAllRole();
        for (Role role_db : role_dbList) {
            System.out.println("====roleController====List()======" + role_db);
        }

        //返回数据
        model.addAttribute("role_dbList", role_dbList);
        //2.跳转页面;
        return "view/system/role/roleList";
    }

    @RequestMapping("/add.do")
    public String add(HttpServletRequest request, HttpServletResponse response, HttpSession session, Model model, Role role, String roleIdStr) {
        //1.接收数据;
        System.out.println("------roleController-------add()--------------" + role);
        //3.调用业务;
        int n = roleService.addRoleByRole(role);

        //2.跳转页面;
        List<Role> role_dbList = roleService.queryAllRole();
        model.addAttribute("role_dbList", role_dbList);
        return "view/system/role/roleList";
    }

    @RequestMapping("/delete.do")
    public String delete(HttpServletRequest request, HttpServletResponse response, HttpSession session, Model model, Role role, String roleIdStr) {
        //1.接收数据;

        //3.调用业务;
        //字符类型转成数字类型
        int roleId = Integer.parseInt(roleIdStr);
        int n = roleService.deleteRoleByRoleId(roleId);
        System.out.println("------roleController-------delete()--------------" + n);
        //2.跳转页面;
        List<Role> role_dbList = roleService.queryAllRole();
        model.addAttribute("role_dbList", role_dbList);
        return "view/system/role/roleList";
    }


    @RequestMapping("/updateUI.do")
    public String updateUI(HttpServletRequest request, HttpServletResponse response, HttpSession session, Model model, Role role, String roleIdStr) {
        //1.接收数据;
        //字符类型转成数字类型
        int roleId = Integer.parseInt(roleIdStr);
        System.out.println("------roleController-------updateUI()--------------" + roleId);
        //3.调用业务;
        Role role_db = roleService.queryRoleByRoleId(roleId);
        model.addAttribute("role_db", role_db);

        //2.跳转页面;
        return "view/system/role/updateRole";
    }

    @RequestMapping("/update.do")
    public String update(HttpServletRequest request, HttpServletResponse response, HttpSession session, Model model, Role role, String roleIdStr) {
        //1.接收数据;
        //字符类型转成数字类型
        int roleId = Integer.parseInt(roleIdStr);
        role.setRoleId(roleId);
        System.out.println("-----------updateUI()--------------" + role);
        //3.调用业务;
        int n = roleService.updateRoleByRole(role);

        //2.跳转页面;
        List<Role> role_dbList = roleService.queryAllRole();
        model.addAttribute("role_dbList", role_dbList);
        return "view/system/role/roleList";
    }


    @RequestMapping("/FPRoleUI.do")
    public String FPRoleUI(HttpServletRequest request, HttpServletResponse response, HttpSession session, Model model, Role role, String roleIdStr, String userIdStr) {
        //1.接收数据;
        int userId = Integer.parseInt(userIdStr);
        //3.调用业务;
        //a.分配角色用户
        User user_db = userService.queryUserByUserId(userId);
        model.addAttribute("user_db", user_db);

        //b.角色列表
        List<Role> role_dbList = roleService.queryAllRole();
        model.addAttribute("role_dbList", role_dbList);


        //d.选中角色数据
        Role role_xz_db = roleService.query_xz_roleByUserId(userId);
        model.addAttribute("role_xz_db", role_xz_db);

        //2.跳转页面;
        return "view/system/role/FPRoleUI";
    }


    @RequestMapping("/FPRole.do")
    public String FPRole(HttpServletRequest request, HttpServletResponse response, HttpSession session, Model model, UserRole userRole, Role role, String roleIdStr, String userIdStr) {
        //1.接收数据;
        //字符类型转数字类型
        int userId = Integer.parseInt(userIdStr);
        int roleId = Integer.parseInt(roleIdStr);
        //3.调用业务;
        //封装
        userRole.setUserId(userId);
        userRole.setRoleId(roleId);
        //分析业务;
        //先删除：（userId）
        int m = userRoleService.deleteUserRoleByUserId(userId);
        //保存
        int n = userRoleService.addUserRoleByUserRole(userRole);

        //2.跳转页面;
        //a.分配角色用户
        User user_db = userService.queryUserByUserId(userId);
        model.addAttribute("user_db", user_db);

        //b.角色列表
        List<Role> role_dbList = roleService.queryAllRole();
        model.addAttribute("role_dbList", role_dbList);

        //c.友情提示
        model.addAttribute("message", "分配角色成功，继续分配吗？");

        //d.选中角色数据
        Role role_xz_db = roleService.query_xz_roleByUserId(userId);
        model.addAttribute("role_xz_db", role_xz_db);

        return "view/system/role/FPRoleUI";
    }
}